package com.shujia.base.javachuantong;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //读取文件
        BufferedReader br = new BufferedReader(new FileReader("hadoop/data/student.txt"));

        //创建一个Map集合存储每一个班级的人数
        HashMap<String, Long> map = new HashMap<>();

        //读取文件每一行数据
        String line = null;
        while ((line = br.readLine()) != null) {
            String clazz = line.split(",")[4];
            //判断集合中是否有该班级作为键，若有，将值拿出来+1，重新赋值回去
            if (map.containsKey(clazz)) {
                map.put(clazz, map.get(clazz) + 1);
            } else {
                map.put(clazz, 1L);
            }
        }

//        map.forEach(new BiConsumer<String, Long>() {
//            @Override
//            public void accept(String s, Long aLong) {
//                System.out.println("班级：" + s + ", 人数：" + aLong);
//            }
//        });

//        map.forEach((k, v) -> System.out.println("班级：" + k + ", 人数：" + v));

        Set<Map.Entry<String, Long>> entries = map.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            System.out.println("班级：" + entry.getKey() + ", 人数：" + entry.getValue());
        }


        br.close();
    }
}
